package com.ss.android.image.model;

import com.bytedance.common.utility.Logger;
import com.ss.android.http.legacy.a.NameValueImpl;
import com.ss.android.image.Image;
import com.ss.android.image.m;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class ImageInfo implements Serializable {
    private static final long serialVersionUID = -6027923654002990158L;
    public volatile transient boolean mDownloaded;
    public int mHeight;
    public Image mImage;
    public transient boolean mIsGif;
    public transient boolean mIsVideo;
    public transient String mKey;
    public transient boolean mNeedAlpha;
    public String mOpenUrl;
    public String mUri;
    public String mUrlList;
    public int mWidth;
    private transient boolean mFixedDisplaySize;

    public ImageInfo(String uri, String urlList) {
        this(uri, urlList, 0, 0, false);
    }

    public ImageInfo(String uri, String urlList, int width, int height, boolean fixedDisplaySize) {
        this.mIsGif = false;
        this.mDownloaded = false;
        this.mIsVideo = false;
        this.mUri = uri;
        this.mUrlList = urlList;
        this.mKey = com.bytedance.common.utility.aa.md5(this.mUri);
        this.mWidth = width;
        this.mHeight = height;
        if (this.mWidth > 0 && this.mHeight > 0) {
            this.mFixedDisplaySize = fixedDisplaySize;
        }
    }

    public ImageInfo(String uri, String urlList, int width, int height) {
        this(uri, urlList, width, height, false);
    }

    public static Image createImage(ImageInfo imageInfo) {
        int v0_3;
        int v2;
        ArrayList v0_2;
        JSONArray v4;
        String v0_1;
        ArrayList v2_2 = null;
        Image v1 = null;
        if (imageInfo == null) {
            return v1;
        }

        try {
            v0_1 = imageInfo.mUrlList;
            if (v0_1 == null) {
                v0_2 = null;
                v2_2 = v0_2;
            } else if (v0_1.length() > 0) {
                v4 = new JSONArray(v0_1);
                int v5 = Math.min(3, v4.length());
                v0_2 = new ArrayList();
                v2 = 0;
                while (true) {
                    if (v2 < v5) {
                        try {
                            JSONObject v6 = v4.optJSONObject(v2);
                            if (v6 != null) {
                                String v6_1 = v6.optString("url");
                                Image.UrlItem v7 = new Image.UrlItem();
                                v7.url = v6_1;
                                ((List) v0_2).add(v7);
                            }
                        } catch (Throwable v2_1) {
                            v2_2 = v0_2;
                            break;
                        }
                        ++v2;
                        continue;
                    }
                    v2_2 = v0_2;
                    break;
                }
            } else {
                v0_2 = null;
                v2_2 = v0_2;
            }
        } catch (Throwable v0) {
            v0_2 = null;
            v2_2 = v0_2;
        }

        v0_1 = imageInfo.mUri;
        v0_1 = v0_1 != null ? v0_1.toLowerCase() : null;
        if (v0_1 != null) {
            if (!v0_1.startsWith("http://") && !v0_1.startsWith("https://")) {
                v0_3 = 0;
            } else {
                v0_3 = 1;
            }
        } else {
            label_67:
            v0_3 = 0;
        }

        if (v0_3 != 0 || !android.support.design.a.isEmptyCollection(((Collection) v2_2))) {
            v1 = new Image();
            v0_1 = v0_3 != 0 ? imageInfo.mUri : ((Image.UrlItem) ((List) v2_2).get(0)).url;
            v1.url = v0_1;
            v1.url_list = ((List) v2_2);
            v1.width = imageInfo.mWidth;
            v1.height = imageInfo.mHeight;
            v0_3 = imageInfo.mIsGif ? 2 : 0;
            v1.type = v0_3;
            if (imageInfo.mIsGif) {
                v1.gif_image_info = imageInfo;
            }
        }

        return v1;
    }

    public static List<ImageUrl> extractImageUrlList(String originUrl, String json) {
        int indext;
        ArrayList<ImageUrl> imageUrlList = new ArrayList();
        if (!android.support.design.a.isTextEmpty(json)) {
            try {
                JSONArray jsonArray = new JSONArray(json);
                int length = jsonArray.length();
                length = Math.min(length, 3);
                indext = 0;
                while (true) {
                    if (indext >= length) {
                        break;
                    } else {
                        JSONObject jsonObject = jsonArray.getJSONObject(indext);
                        String url = jsonObject.getString("url");
                        if (!android.support.design.a.isTextEmpty(url)) {
                            ImageUrl imageUrl = new ImageUrl(url);
                            imageUrlList.add(imageUrl);
                            JSONObject headerJO = jsonObject.optJSONObject("header");
                            if (headerJO != null) {
                                Iterator<String> keysIterator = headerJO.keys();
                                while (true) {
                                    if (keysIterator.hasNext()) {
                                        String key = keysIterator.next();
                                        String value = headerJO.getString(key);
                                        if (android.support.design.a.isTextEmpty(key)) {
                                            continue;
                                        }

                                        if (android.support.design.a.isTextEmpty(value)) {
                                            continue;
                                        }

                                        imageUrl.headers.add(new NameValueImpl(key, value));
                                        continue;
                                    }
                                    ++indext;
                                    break;
                                }
                            }
                        }
                        break;
                    }
                }
            } catch (JSONException jsonException) {
                Logger.v("ImageInfo", "extract url_list exception: " + jsonException);
            }
        }

        if (imageUrlList.isEmpty() && !android.support.design.a.isTextEmpty(originUrl) && (m.a().isValidUrl(originUrl.toLowerCase()))) {
            imageUrlList.add(new ImageUrl(originUrl));
        }
        return imageUrlList;
    }

    public static ImageInfo fromJson(JSONObject jo, boolean checkWidth) {
        if (jo != null) {
            String uri = jo.optString("uri");
            String url_list = jo.optString("url_list");
            int width = jo.optInt("width");
            int height = jo.optInt("height");
            if (!android.support.design.a.isTextEmpty(uri)) {
                if (checkWidth) {
                    if (width <= 0) {
                        return null;
                    } else if (height > 0) {

                    } else {
                        return null;
                    }
                }

                ImageInfo imageInfo = new ImageInfo(uri, url_list, width, height);
                imageInfo.mOpenUrl = jo.optString("open_url", null);
                imageInfo.mImage = ImageInfo.createImage(imageInfo);
                return imageInfo;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    public static ImageInfo fromJsonStr(String json) {
        return ImageInfo.fromJsonStr(json, true);
    }

    public static ImageInfo fromJsonStr(String json, boolean checkWidth) {
        if (!android.support.design.a.isTextEmpty(json)) {
            try {
                return ImageInfo.fromJson(new JSONObject(json), checkWidth);
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }

        return null;
    }

    public static String getUrlFromImageInfo(ImageInfo imageInfo, boolean arg4) {
        String v1 = null;
        if (imageInfo != null) {
            String url = arg4 ? imageInfo.mUri : null;
            List v0_1 = ImageInfo.extractImageUrlList(url, imageInfo.mUrlList);
            if (v0_1 == null) {
                return v1;
            }

            if (v0_1.size() <= 0) {
                return v1;
            }

            v1 = ((ImageUrl) v0_1.get(0)).url;
        }

        return v1;
    }

    public static ArrayList optImageList(JSONObject arg1, String arg2, boolean arg3) {
        ArrayList v0 = arg1 == null || arg2 == null || arg2.length() == 0 || !arg1.has(arg2) ? null : ImageInfo.optImageList(arg1.optJSONArray(arg2), arg3);
        return v0;
    }

    public static ArrayList optImageList(JSONArray jsonArray, boolean arg5) {
        ArrayList v0 = null;
        if (jsonArray != null && jsonArray.length() != 0) {
            int v2 = jsonArray.length();
            int v1;
            for (v1 = 0; v1 < v2; ++v1) {
                JSONObject v3 = jsonArray.optJSONObject(v1);
                if (v3 != null) {
                    ImageInfo v3_1 = ImageInfo.fromJson(v3, arg5);
                    if (v3_1 != null) {
                        if (v0 == null) {
                            v0 = new ArrayList();
                        }

                        v0.add(v3_1);
                    }
                }
            }
        }

        return v0;
    }

    public static List<ImageInfo> parseImageList(JSONArray imageInfoJA, boolean checkWidth) throws JSONException {
        ArrayList<ImageInfo> imageInfoList = new ArrayList();
        int length = imageInfoJA.length();
        for (int i = 0; i < length; ++i) {
            ImageInfo imageInfo = ImageInfo.fromJson(imageInfoJA.getJSONObject(i), checkWidth);
            if (imageInfo != null) {
                imageInfoList.add(imageInfo);
            }
        }
        return imageInfoList;
    }

    public static JSONArray toJsonArray(List arg3) {
        JSONArray v0_1;
        if (arg3 == null || arg3.size() == 0) {
            v0_1 = null;
        } else {
            JSONArray v1 = new JSONArray();
            Iterator v2 = arg3.iterator();
            while (v2.hasNext()) {
                JSONObject v0 = ((ImageInfo) v2.next()).toJsonObj();
                if (v0 == null) {
                    continue;
                }

                v1.put(v0);
            }

            v0_1 = v1;
        }

        return v0_1;
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        ImageInfo imageInfo = (ImageInfo) obj;
        if (this.mWidth != imageInfo.mWidth) {
            return false;
        }
        if (this.mHeight != imageInfo.mHeight) {
            return false;
        }
        if (this.mIsGif != imageInfo.mIsGif) {
            return false;
        }
        if (this.mDownloaded != imageInfo.mDownloaded) {
            return false;
        }
        if (this.mIsVideo != imageInfo.mIsVideo) {
            return false;
        }
        if (this.mFixedDisplaySize != imageInfo.mFixedDisplaySize) {
            return false;
        }
        if (this.mUri == null ? imageInfo.mUri != null : !this.mUri.equals(imageInfo.mUri)) {
            return false;
        }
        if (this.mOpenUrl == null ? imageInfo.mOpenUrl != null : !this.mOpenUrl.equals(imageInfo.mOpenUrl)) {
            return false;
        }
        if (this.mUrlList == null ? imageInfo.mUrlList != null : !this.mUrlList.equals(imageInfo.mUrlList)) {
            return false;
        }
        if (this.mKey != null) {
            if (this.mKey.equals(imageInfo.mKey)) {
                return true;
            }
        } else if (imageInfo.mKey == null) {
            return true;
        }
        return false;
    }


    public boolean isFixedDisplaySize() {
        return this.mFixedDisplaySize;
    }

    public boolean isValid() {
        return this.mWidth > 0 && this.mHeight > 0 && (!android.support.design.a.isTextEmpty(this.mUri));
    }

    public JSONObject toJsonObj() {
        JSONObject v1_1;
        JSONObject v0 = null;
        if (!android.support.design.a.isTextEmpty(this.mUri)) {
            try {
                v1_1 = new JSONObject();
                v1_1.put("uri", this.mUri);
                v1_1.put("width", this.mWidth);
                v1_1.put("height", this.mHeight);
                if (this.mOpenUrl != null) {
                    v1_1.put("open_url", this.mOpenUrl);
                }

                boolean v0_1 = android.support.design.a.isTextEmpty(this.mUrlList);
                if (v0_1) {
                    return v1_1;
                }
            } catch (Exception v1) {
                return v0;
            }

            try {
                v1_1.put("url_list", new JSONArray(this.mUrlList));
            } catch (Exception v0_2) {
            }
            v0 = v1_1;
        }

        return v0;
    }
}

